package com.notificationtester;

import android.app.Activity;
import android.os.Bundle;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.notificationtester.NotificationService;

public class MainNotifier extends Activity {
	
	private PendingIntent mAlarmSender;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        mAlarmSender = PendingIntent.getService(MainNotifier.this,
                0, new Intent(MainNotifier.this, NotificationService.class), 0);
        
        setContentView(R.layout.main);
        
        Button button = (Button)findViewById(R.id.start_notifications);
        button.setOnClickListener(mStartListener);
        button = (Button)findViewById(R.id.stop_notifications);
        button.setOnClickListener(mStopListener);
    }
    
    private OnClickListener mStartListener = new OnClickListener() { 
    	 public void onClick(View v) {
    		 
             long firstTime = SystemClock.elapsedRealtime();

             AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
             am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                             firstTime, 30*1000, mAlarmSender);

             Toast.makeText(MainNotifier.this, R.string.repeating_scheduled,
                     Toast.LENGTH_LONG).show();
         }
    };
    
    private OnClickListener mStopListener = new OnClickListener() {
        public void onClick(View v) {
            // And cancel the alarm.
            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
            am.cancel(mAlarmSender);

            // Tell the user about what we did.
            Toast.makeText(MainNotifier.this, R.string.repeating_unscheduled,
                    Toast.LENGTH_LONG).show();

        }
    };
}